package com.jinlong.untils;

import com.jinlong.common.entity.TestEntity;
import org.apache.ibatis.jdbc.SQL;

import java.lang.reflect.Field;
import java.sql.Timestamp;

/**
 * 反射工具类
 *
 */
public class ReflectionUntils {

    /**
     * 获取到父类和子类的属性
     * @param oj
     * @return
     */
    public static String FatherAndSon(Object oj){
        if(oj == null){
            return null;
        }
        Class classInfo = oj.getClass();
        //获取父类的属性
        Field[] fields = classInfo.getSuperclass().getDeclaredFields();

        //获取当前类的属性集合
        Field[] fields1 = classInfo.getDeclaredFields();

        return getFields(fields)+","+getFields(fields1);
    }

    /**
     * 获取到父类和子类的值
     * @param oj
     * @return
     */
    public static String FatherAndSonValue(Object oj){
        if(oj == null){
            return null;
        }
        Class classInfo = oj.getClass();
        //获取父类的属性
        Field[] fields = classInfo.getSuperclass().getDeclaredFields();

        //获取当前类的属性集合
        Field[] fields1 = classInfo.getDeclaredFields();

        return getFieldsValue(oj,fields)+","+getFieldsValue(oj,fields1);
    }

    public static String getFields(Field[] fields){
        StringBuffer bf = new StringBuffer();
        for (int i = 0;i<fields.length;i++){
            bf.append(fields[i].getName());
            /*for(int j=0;j<fields[i].getName().length();j++){
                char c = fields[i].getName().charAt(j);
                if(Character.isLowerCase(c)){
                    bf.append(c);
                }else{
                    bf.append("_");
                    bf.append((char)(c+32));
                   // bf.append(c+32);
                }
            }*/


                if(i<fields.length-1){
                bf.append(",");
            }
        }

        return bf.toString();
    }

    public static String getFieldsValue(Object oj,Field[] fields){
        StringBuffer bf = new StringBuffer();
        for (int i = 0;i<fields.length;i++){
            try {
                //允许操作私有属性
                fields[i].setAccessible(true);

                Object value = fields[i].get(oj);
                //标识类型是否为String类型的
                boolean flag = false;
                if(value !=null &&(value instanceof String || value instanceof Timestamp)){
                    flag = true;
                }
                if(flag){
                    bf.append("'");
                    bf.append(value);
                    bf.append("'");
                }else {
                    bf.append(value);
                }
              //bf.append(fields[i].get(oj));
                /*if(fields[i].get(oj) instanceof String){
                    bf.append("'");
                    bf.append(fields[i].get(oj));
                    bf.append("'");
                }else {
                    bf.append(fields[i].get(oj));
                }*/
                if(i<fields.length-1){
                    bf.append(",");
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }

        }

        return bf.toString();
    }
    /*public static void main(String[] args) {
        TestEntity testEntity = new TestEntity();
        testEntity.setAge(20);
        testEntity.setPhone("123456456");
        testEntity.setUserName("jinlong");
        testEntity.setPassWord("123123");
        testEntity.setCreatedTime(DataUtils.getTimestamp());
        testEntity.setUpdateTime(DataUtils.getTimestamp());
        String name = FatherAndSon(testEntity);
        String value = FatherAndSonValue(testEntity);
        System.out.println(name);
        System.out.println(value);

        SQL sql =new SQL(){{
            INSERT_INTO("mb_user");
            VALUES(name,value);
        }};
        System.out.println(sql.toString());
    }*/
}
